package com.turing.post.datatranfer.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.capitalaccounts.util.CapitalApplicationException;
import com.turing.post.common.util.ContrastDao;
import com.turing.post.common.util.SysOpUser;
import com.turing.post.common.util.SysOpUserBusiness;
import com.turing.post.common.util.SysOpUserDao;
import com.turing.post.datatranfer.util.OldUserBusiness;
import com.turing.post.datatranfer.util.OldUserDao;
import com.turing.post.datatranfer.util.TranferApplicationException;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * 用户业务实现接口
 * 
 * @author WZD
 */
@Stateless(mappedName = "datatranfer.impl.OldUserBusinessImpl")
@Remote
@Local
public class OldUserBusinessImpl implements OldUserBusiness {

	@EJB(mappedName = "datatranfer.impl.OldUserDaoImpl")
	private OldUserDao oldUserDaoImpl;
	@EJB(mappedName = "common.impl.SysOpUserBusinessImpl")
	private SysOpUserBusiness sysOpUserBusinessImpl;// 用户接口

	@EJB(mappedName = "common.impl.SysOpUserDaoImpl")
	private SysOpUserDao sysOpUserDaoImpl;
	
	@EJB(mappedName = "common.impl.ContrastDaoImpl")
	private ContrastDao contrastDaoImpl;// 对照表接口
	public void insertNewUser(PCPSWebInput webInput1,String branchno,String issuecompanyno,String roleid) throws PCPSApplicationException{
		List<Object[]> list = oldUserDaoImpl.getSys_Oper();
		SysOpUser sysOpUser = null;
		Object[] obj = null;
		PCPSWebInput webInput=new PCPSWebInput();
		webInput.setOperBranchNo(issuecompanyno);
		webInput.setOperId(new BigInteger("9"));
		for (int i = 0; i < list.size(); i++) {
			obj = list.get(i);
			
			// 检查发卡商机构是否存在
			if (PCPSUtil.isEmpty(branchno)) {
				throw new CapitalApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "机构号为空!");
			}	
			// 检查发卡商机构是否存在
			if (PCPSUtil.isEmpty(issuecompanyno)) {
				throw new CapitalApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "发卡商号为空!");
			}	
			if (PCPSUtil.isEmpty(roleid)) {
				throw new CapitalApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "角色为空!");
			}	
			String username=issuecompanyno+"_"+obj[0].toString().trim();
			if(PCPSUtil.isEmpty(contrastDaoImpl.getMapSysOperuserByNo(username))){
				if(sysOpUserDaoImpl.getQuestionByByOperLogonName(username)==null){
				   sysOpUser = new SysOpUser();				
				   sysOpUser.setOperLogonName(username);
				   sysOpUser.setOperPasswd(username+"1");
				   sysOpUser.setOperBranchNo(contrastDaoImpl.getMapIssueBranchByNo(obj[2].toString().trim()));
//				   sysOpUser.setRoleId(new BigInteger(roleid));
				   sysOpUser.setName(obj[1].toString().trim());
				 // System.out.println(sysOpUser.getName()+"__"+sysOpUser.getName().length());
				   sysOpUser.setSex("0");
				   sysOpUser.setStatus(obj[3].toString());
				   sysOpUser.setIssueOper("0");
				   sysOpUser.setInternetAgent("0");
				   sysOpUser.setPwdErrCnt(0);
				   sysOpUserBusinessImpl.insertSysOpUser(webInput, sysOpUser);
				   SysOpUser returnsysOpUser=sysOpUserDaoImpl.getSysOpUserByOperLogonName(username);
				   contrastDaoImpl.insertMapSysOperuser(obj[0].toString(), returnsysOpUser.getOperId().toString());
				}
			}
		}
	}
}
